#include <REGX52.H>
#include <INTRINS.H>
#include "Delay.h"
#include "Timer0.h"
#include "Blueteech.h"
#include "Key.h"
#include "oled.h"
#include "music.h"
sbit DJ=P1^7;                	//舵机
sbit DWJ=P1^2;								//电位器
unsigned char Keynum = 0;      //矩阵按键编码
unsigned char enter[7]={0};	//输入 enter[0]为输入位置
unsigned char pass[7]={0}; 	//初始密码为000000 pass[0]修改密码定位
char n;  										//遍历输入数组
extern unsigned char dtime;
bit flag=1,end=0;            //密码修改与电位器开关锁状态指示
	void Timer2_Init(void)		//20毫秒
{
	T2MOD = 0;		//初始化模式寄存器
	T2CON = 0;		//初始化控制寄存器
	TL2 = 0xE0;		//设置定时初始值
	TH2 = 0xB1;		//设置定时初始值
	RCAP2L = 0xE0;		//设置定时重载值20ms
	RCAP2H = 0xB1;		//设置定时重载值
	ET2=1;
	EA=1;
	PT2=0;
	TR2 = 1;		//定时器2开始计时
}

void main(void)
{
	OLED_Init();                        //oled初始化
	Delay(100);
	OLED_CLEAR();												//清屏
	Delay(100);
	Timer0_Init();
	Timer2_Init();
	Uart_Init();
	OLED_ShowString(10,6,"A Door Lock");
	while(1)
	{ 
		flag = 1;																	//密码修改状态flag
		Keynum = Key();
		if(Keynum<=10&&Keynum!=0&&enter[0]<6){   
			enter[0]++;															//enter[0]定位输入密码位数
			music(Musicsix,500);										//播放提示音一
			if(Keynum==10){
				enter[enter[0]] = 0;   								//输入0
			}
			if(Keynum!=10){
				enter[enter[0]] = Keynum;  						//输入1-9
			}	
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //enter[0]定位 一位位显示输入密码

		}
		if(Keynum==11&&enter[0]>=1){  						//Delete回退一位
			enter[enter[0]] = 0;
			OLED_ShowString(22+8*enter[0],1," ");  	//空白覆盖回退的一位
			enter[0]--;                              
			music(Musicsev,500);										//播放提示音二
		}
		
		if(Keynum==16){														//进入密码修改模式
			OLED_ShowString(30,3,"modifing...");
			TR2 = 0;																//关闭定时器2矩阵按键扫描
			OLED_ShowString(30,1,"       ");				//清屏
			music(Musicone,400);										//播放一段音乐3——4s
			for(n=6;n--;n>=0){											
				enter[n]=0;   												//遍历 清零原先输入密码
			}	
			TR2 = 1;																//开启矩阵按键扫描
			while(flag){														
				
				Keynum = Key();
			if(Keynum<=10&&Keynum!=0&&pass[0]<6){   
			pass[0]++;
			music(Musicsix,500);										
			if(Keynum==10){
				pass[pass[0]] = 0;   									//输入0
			}
			if(Keynum!=10){
				pass[pass[0]] = Keynum;  							//输入1-9
			}	
			OLED_ShowNum(22+8*pass[0],1,pass[pass[0]],1,16);  //pass[0]定位一位位显示输入密码

		}
		if(Keynum==11&&pass[0]>=1){  						//Delete回退一位
			pass[pass[0]] = 0;
			OLED_ShowString(22+8*pass[0],1," ");  //空白覆盖回退的一位
			pass[0]--;
			music(Musicsev,500);
		}
		if(Keynum==12&&pass[0]==6){ 	   		  	//确认密码修改完成	
			OLED_ShowString(30,3,"            ");
			Delay(5);
			OLED_ShowString(30,3,"Complete!");
			TR2 = 0;
			flag = 0;															//退出密码修改状态flag
			pass[0] = 0;
			music(Musictwo,500);                  //播放一段音乐
			OLED_ShowString(30,1,"       ");			//清空密码输入 oled
			OLED_ShowString(30,3,"          ");
			TR2 = 1;
		}
	}
	}
		
		
		if(Keynum==12&&enter[0]==6){ 					//Enter校验密码 确定开锁
			TR2 = 0;
			if(enter[1]==pass[1]&&enter[2]==pass[2]&&enter[3]==pass[3]&&enter[4]==pass[4]&&enter[5]==pass[5]&&enter[6]==pass[6])
			{
				OLED_ShowString(30,3,"Right!");
			dtime = 7;
			while(dtime){												//开锁舵机角度180
			DJ=1;
			Delay500us();
			DJ=0;
			Delay(20);
			dtime--;
		}	music(Musicfive,714);				//密码正确音乐
			
			}else{OLED_ShowString(30,3,"Wrong!");
				music(Musicthr,500);          //密码错误音乐
				
				
			}
			OLED_ShowString(30,1,"       ");			//清空密码输入oled
			for(n=6;n--;n>=0){	
				enter[n]=0;   											//清零输入密码
			}
			OLED_ShowString(30,3,"       ");
			TR2 = 1;
		}
		if(Keynum==13){													//角度135度
			TR2 = 0;
			music(Musicsix,500);                  //按键音
			dtime = 7;
			while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
		TR2 = 1;
		}
		if(Keynum==14){														
			TR2 = 0;															//角度90度
			music(Musicsix,500);
			dtime = 7;
			while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
		TR2 = 1;
		}

		if(Keynum==15){														//关锁舵机角度0度
			OLED_ShowString(30,3,"Close!");
			TR2 = 0;
			music(Musicsix,500);
			dtime = 7;
			while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
		OLED_ShowString(30,3,"       ");
		TR2 = 1;
		}
		if(DWJ==0&&end==0){  
			OLED_ShowString(30,3,"Open!");
			TR2=0;
			dtime = 7;
			end = 1;                                //已开锁状态值1
			while(dtime){														//电位器开锁舵机角度180
			DJ=1;
			Delay500us();
			DJ=0;
			Delay(20);
			dtime--;
		}	music(Musicsev,500);                    //播放提示音
			OLED_ShowString(30,3,"      ");
		TR2=1;
		}
		if(DWJ==1&&end){													//钥匙开锁时关锁
			OLED_ShowString(30,3,"Close!");
			end = 0;                                //关锁状态值0
			TR2 = 0;
			music(Musicsev,500);                    //播放提示音
			dtime = 7;                              //舵机关锁0度
			while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
		OLED_ShowString(30,3,"       ");
		TR2 = 1;
		}
	}
} 	

	

void Timer2_Routine() interrupt 5
{	  
	TF2=0;
	RCAP2L = 0xE0;		//设置定时重载值20ms
	RCAP2H = 0xB1;		//设置定时重载值
	Key_Loop();

}
